package me.zhengjie.minbearwcs.serivce.mission.generate.outbound;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.minbearwcs.dao.mission.inf.WcsMissionDao;
import me.zhengjie.minbearwcs.dao.outbound.inf.OutboundTaskRecordDao;
import me.zhengjie.minbearwcs.dao.stock.inf.PalletDao;
import me.zhengjie.minbearwcs.dao.storage.inf.LargeStorageDao;
import me.zhengjie.minbearwcs.dao.storage.inf.MediumStorageDao;
import me.zhengjie.minbearwcs.dao.storage.inf.SmallStorageDao;
import me.zhengjie.minbearwcs.dao.storage.inf.StorageDao;
import me.zhengjie.minbearwcs.entity.DTO.transfer.Tuple;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.WcsMission;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.status.OutboundAfterSortMissionStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.status.OutboundSortExceptionStatus;
import me.zhengjie.minbearwcs.entity.PO.wcs.mission.type.MissionType;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.common.OutboundContentPool;
import me.zhengjie.minbearwcs.entity.PO.wcs.outbound.record.OutboundTaskRecord;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.Pallet;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.LargeStorage;
import me.zhengjie.minbearwcs.entity.PO.wcs.storage.SmallStorage;
import me.zhengjie.minbearwcs.serivce.mission.generate.MissionGenerator;
import me.zhengjie.minbearwcs.serivce.mission.generate.type.MissionGenType;
import me.zhengjie.minbearwcs.utils.IdWorker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Slf4j
@Component
@RequiredArgsConstructor
public class OutboundSortExceptionMissionGenerator implements MissionGenerator<String> {

    private final OutboundTaskRecordDao outboundTaskRecordDao;
    private final WcsMissionDao wcsMissionDao;
    private final IdWorker idWorker;
    private final SmallStorageDao smallStorageDao;
    private final MediumStorageDao mediumStorageDao;
    private final LargeStorageDao largeStorageDao;
    private final PalletDao palletDao;
    private final StorageDao storageDao;


    @Override
    public MissionGenType type() {
        return MissionGenType.OUTBOUND_SORT_EXCEPTION;
    }

    @Override
    @Transactional
    public Tuple<String> generate(Object source) {
        OutboundTaskRecord oldOutboundTaskRecord = (OutboundTaskRecord) source;

        SmallStorage smallStorage = smallStorageDao.selectByPallet(oldOutboundTaskRecord.getPalletCode());
        String largeStorageCode = storageDao.selectLargeStorageCodeBySmallStorageCode(smallStorage.getSmallStorageCode());
        LargeStorage largeStorage = largeStorageDao.selectByLargeStorageCode(largeStorageCode);

        Pallet pallet = palletDao.selectByPalletCode(smallStorage.getPalletCode());

        // 生成有起点的任务
        OutboundTaskRecord outboundTaskRecord = new OutboundTaskRecord(null, "outbound_record_" + idWorker.nextId(), null,
                null, smallStorage.getPalletCode(), null,
                null, largeStorage.getFloorNum(), smallStorage.getSmallStorageCode(), null, null,
                OutboundContentPool.OUTBOUND_RECORD_NOT_SORT, null, LocalDateTime.now(),
                oldOutboundTaskRecord.getOriginalSmallStorage(),pallet.getLogisticsCompanyCode());

        String missionId = "Task_Outbound_Sort_Exception_" + idWorker.nextId();

        WcsMission wcsMission = new WcsMission(null, missionId, null, MissionType.OUTBOUND_SORT_EXCEPTION.name(),
                OutboundSortExceptionStatus.WAIT_FOR_STORAGE.name(), LocalDateTime.now(), null, null,
                1, outboundTaskRecord.getOutboundRecordId());
        wcsMissionDao.insert(wcsMission);
        outboundTaskRecordDao.insert(outboundTaskRecord);


        return Tuple.tuple(null,true);
    }
}
